home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / SLIDING_.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  11.9 KB  |  356 lines

  1. package sub_arctic.lib;
  2.  
  3. import sub_arctic.lib.oneline_text_display;
  4. import sub_arctic.lib.manager;
  5. import sub_arctic.output.color_pair;
  6. import sub_arctic.output.drawable;
  7. import sub_arctic.input.event;
  8. import sub_arctic.input.callback_object;
  9. import sub_arctic.anim.trajectory;
  10. import sub_arctic.anim.transition;
  11. import sub_arctic.anim.animatable;
  12. import java.awt.Point;
  13. import java.awt.Rectangle;
  14. import java.awt.Font;
  15. import java.awt.FontMetrics;
  16. import java.awt.Color;
  17.  
  18. /**
  19.  * This is a class which draws a shifting portion of a long string on the 
  20.  * screen to make "sliding sign". This object has options for putting a box 
  21.  * around the text, making it be opaque, setting its colors, and of course 
  22.  * controlling the displayed string and position within the string.
  23.  * 
  24.  * @author Scott Hudson
  25.  */
  26. public class sliding_text extends label implements animatable {
  27.  
  28.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  29.  
  30.   /** Identifier for callback at end of scroll */
  31.   public static int END_OF_SCROLL = 0;
  32.  
  33.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  34.  
  35.   /** Object we make callbacks to at the end of a scroll */
  36.   protected callback_object _callback_obj = null;
  37.  
  38.   /** 
  39.    * Object that we make callbacks to at the end of a scroll.  All callbacks 
  40.    * have a callback number END_OF_SCROLL and the rest of the fields are
  41.    * unused.
  42.    * 
  43.    * @return callback_object the object sends the callbacks to.
  44.    */
  45.   public callback_object callback_obj() {return _callback_obj;}
  46.  
  47.   /** 
  48.    * Object that we make callbacks to at the end of a scroll.  All callbacks 
  49.    * have a callback number END_OF_SCROLL and the rest of the fields are
  50.    * unused.
  51.    */
  52.   public void set_callback_obj(callback_object cb) {_callback_obj = cb;}
  53.  
  54.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  55.  
  56.   /**
  57.    * Make a object using some simple defaults. It doesn't try to position 
  58.    * itself. It assumes you'll do this with constraints. It sizes height by
  59.    * the size of the text and width is set to a default.
  60.    *
  61.    * @param String          s  the string to put in the display
  62.    * @param callback_object cb object to call back at end of scroll
  63.    */
  64.   public sliding_text(String s, callback_object cb) {
  65.     super(s);
  66.     set_w(250);
  67.     set_boxed(true);
  68.     set_autosize(false);
  69.     set_callback_obj(cb);
  70.   }
  71.  
  72.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  73.  
  74.   /** 
  75.    * Again, make an object with some defaults. This time you supply the
  76.    * width. Assumes x and y will use constraints. 
  77.    * 
  78.    * @param String s the string to display 
  79.    * @param int width the width of the displayable area in pixels
  80.    * @param callback_object cb object to call back at end of scroll
  81.    */
  82.   public sliding_text(String s, int width, callback_object cb) {
  83.     super(s,width);
  84.     set_boxed(true);
  85.     set_autosize(false);
  86.     set_callback_obj(cb);
  87.   }
  88.  
  89.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  90.  
  91.   /** 
  92.    * Again, make an object with some defaults. This time you supply the
  93.    * width & a font. Assumes x and y will use constraints.
  94.    * 
  95.    * @param String s the displayed string
  96.    * @param int width the number of pixels wide the display area is
  97.    * @param Font f the font render the string in
  98.    * @param callback_object cb object to call back at end of scroll
  99.    */
  100.   public sliding_text(String s, int width, Font f, callback_object cb) {
  101.     super(s,width,f);
  102.     set_boxed(true);
  103.     set_autosize(false);
  104.     set_callback_obj(cb);
  105.   }
  106.  
  107.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  108.  
  109.   /** 
  110.    * Again, make a label with some defaults. This time you supply the
  111.    * font. Assumes x and y will use constraints.
  112.    *  
  113.    * @param String s the displayed string
  114.    * @param Font f the font to use for rendering the string
  115.    * @param callback_object cb object to call back at end of scroll
  116.    */
  117.   public sliding_text(String s, Font f, callback_object cb) {
  118.     super(s,f);
  119.     set_w(250);
  120.     set_boxed(true);
  121.     set_autosize(false);
  122.     set_callback_obj(cb);
  123.   }
  124.  
  125.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  126.  
  127.   /** Current offset from left edge to the first displayed character (which
  128.    *  has index first_vis_char within the string).
  129.    */
  130.   protected int x_offset = 0;
  131.  
  132.   /** 
  133.    * Shift the text left by the given amount (which must be positive!). 
  134.    * @param int dist the amount to shift.
  135.    */
  136.   protected void shift_text_left(int dist)
  137.     {
  138.       x_offset += Math.max(dist,0);
  139.       trim_front_chars();
  140.       damage_self(); 
  141.     }
  142.  
  143.   /** Index of the first character that is visible under the current offset */
  144.   protected int first_vis_char = 0;
  145.  
  146.   /** 
  147.    * Potentially adjust first_vis_char based on new offset.  If this moves
  148.    * the index over, it will also move over the offset by the width of the 
  149.    * characters removed
  150.    */
  151.   protected void trim_front_chars()
  152.     {
  153.       char ch;
  154.       int  ch_w, i;
  155.  
  156.       for (i = first_vis_char; i < text().length(); i++)
  157.     {
  158.       /* measure size of first character */
  159.       ch = text().charAt(i);
  160.       ch_w = _metric.charWidth(ch);
  161.  
  162.       /* if the offset is more than the first character, snip it off */
  163.       if (ch <= x_offset)
  164.         {
  165.           x_offset -= ch_w;
  166.           first_vis_char++;
  167.         }
  168.       else
  169.         {
  170.           /* that character extends past the start of the visible string 
  171.            * so we can stop looking. */
  172.           break;
  173.         }
  174.     }
  175.     }
  176.  
  177.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  178.  
  179.   /** 
  180.    * Override to reset offset when text is replaced 
  181.    * @param String txt new text to display.
  182.    */
  183.   public void set_text(String txt)
  184.     {
  185.       /* let super class do its thing */
  186.       super.set_text(txt);
  187.  
  188.       /* reset our bookkeeping to put string back at the beginning */
  189.       x_offset = 0;
  190.       first_vis_char = 0;
  191.     }
  192.  
  193.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  194.  
  195.   /** 
  196.    * Return the length of the total display string (in pixels).  This is the
  197.    * complete string, not just the portion displayed.
  198.    *
  199.    * @return int length of string.
  200.    */
  201.   public int text_length()
  202.     {
  203.       return _metric.stringWidth(text());
  204.     }
  205.  
  206.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  207.  
  208.   /**
  209.    * Handle start of animation transition.  This transition is used to 
  210.    * drive calls to shift_text_left() to animate the display.
  211.    *
  212.    * @param transition trans     the transition object controlling this.
  213.    * @param trajectory traj      the trajectory it is working over.
  214.    * @param double     start_t   start value along trajectory 0..1.
  215.    * @param Object     start_obj first data value out of trajectory (must be 
  216.    *                             a Point object).
  217.    * @param event      e         event "causing" the animation.
  218.    * @param Object     user_info the information associated with then object 
  219.    *                             when the transition was scheduled.
  220.    */
  221.   public void start_transition(
  222.     transition trans, trajectory traj,
  223.     double start_t,   Object start_obj, 
  224.     event e,          Object user_info) 
  225.     {
  226.       /* nothing to do here */
  227.     }
  228.  
  229.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  230.  
  231.   /**
  232.    * Handle an animation step.  This animation should be along a trajectory of
  233.    * points, and the x value are used to drive calls to shift_text_left().
  234.    * 
  235.    * @param transition trans     the transition object controlling this.
  236.    * @param trajectory traj      the trajectory it is working over.
  237.    * @param double     start_t   start value of this step (within 0..1 overall)
  238.    * @param Object     start_obj start data value for this step (this must be 
  239.    *                             a Point object).
  240.    * @param double     end_t     end value of this step (within 0..1 overall)
  241.    * @param Object     end_obj   end data value for this step (this must be 
  242.    *                             a Point object).
  243.    * @param event      e         event "causing" the animation.
  244.    * @param Object     user_info the information associated with then object 
  245.    *                             when the transition was scheduled.
  246.    */
  247.   public void transition_step(
  248.     transition trans, trajectory traj,
  249.     double start_t, Object start_obj, 
  250.     double end_t, Object end_obj,
  251.     event e, Object user_info) 
  252.     {
  253.       shift_text_left( ((Point)end_obj).x - ((Point)start_obj).x );
  254.     }
  255.  
  256.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  257.  
  258.   /**
  259.    * Handle the end of the animation transition.  This animation should be 
  260.    * along a trajectory of points, and the x value are used to drive calls to 
  261.    * shift_text_left().  The callback END_OF_SCROLL is made at this point.
  262.    * 
  263.    * @param transition trans     the transition object controlling this.
  264.    * @param trajectory traj      the trajectory it is working over.
  265.    * @param double     start_t   start value of this step (within 0..1 overall)
  266.    * @param Object     start_obj start data value for this step (this must be 
  267.    *                             a Point object).
  268.    * @param double     end_t     end value of this step (within 0..1 overall)
  269.    * @param Object     end_obj   end data value for this step (this must be 
  270.    *                             a Point object).
  271.    * @param event      e         event "causing" the animation.
  272.    * @param Object     user_info the information associated with then object 
  273.    *                             when the transition was scheduled.
  274.    */
  275.   public void end_transition(
  276.     transition trans, trajectory traj,
  277.     double start_t, Object start_obj,
  278.     double end_t, Object end_obj,
  279.     event e, Object user_info) 
  280.     {
  281.       /* do the end scroll callback */
  282.       if (callback_obj() != null)
  283.     callback_obj().callback(this, e, END_OF_SCROLL, null);
  284.     }
  285.  
  286.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  287.  
  288.   /** 
  289.    * Draw the display 
  290.    * @param drawable d the drawable on which to do the display
  291.    */
  292.   protected void draw_self_local(drawable d) 
  293. {
  294.       Rectangle clip_rect;
  295.       int draw_index, draw_loc, draw_size;
  296.       int clip_right, str_len;
  297.       String str;
  298.  
  299.       /* clear the background if they asked for that */
  300.       if (opaque())
  301.     {
  302.           d.setColor(draw_colors().background());
  303.           d.fillRect(0,0,w()-1,h()-1);
  304.     }
  305.  
  306.       /* set font to our font, and start drawing in foreground */
  307.       d.setFont(font());
  308.       d.setColor(draw_colors().foreground());
  309.  
  310.       /* draw the text in 10 character chunks starting at first_vis_char until 
  311.        *  we run off the end of the clipping area 
  312.        */
  313.       str_len = text().length();
  314.       draw_index = first_vis_char; 
  315.       clip_rect = d.getClipRect();
  316.       clip_right = clip_rect.x + clip_rect.width;
  317.       for (draw_loc = h_spacing()-x_offset; 
  318.        draw_loc <= clip_right && draw_index < str_len; 
  319.        draw_loc += draw_size)
  320.     {
  321.       /* pull out the chunk of string and draw it */
  322.           str = text().substring(draw_index, Math.min(draw_index+10,str_len));
  323.           d.drawString(str, draw_loc, v_spacing()+_metric.getAscent());
  324.  
  325.       /* set up for the next chunk */
  326.       draw_index += 10;
  327.           draw_size = _metric.stringWidth(str);
  328.     }
  329.  
  330.       /* draw box if requested */
  331.       if (boxed())
  332.         d.drawRect(0,0,w()-1,h()-1);
  333.     }
  334.  
  335.    //had:
  336.    //* @exception general PROPAGATED
  337.  
  338.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  339. }
  340. /*=========================== COPYRIGHT NOTICE ===========================
  341.  
  342. This file is part of the subArctic user interface toolkit.
  343.  
  344. Copyright (c) 1996 Scott Hudson and Ian Smith
  345. All rights reserved.
  346.  
  347. The subArctic system is freely available for most uses under the terms
  348. and conditions described in 
  349.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  350. and appearing in full in the lib/interactor.java source file.
  351.  
  352. The current release and additional information about this software can be 
  353. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  354.  
  355. ========================================================================*/
  356.